<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>健康服务系统</title>
    <meta name="description" content="健康服务系统">
    <meta name="keywords" content="健康服务系统">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>健康咨讯<small>健康咨讯</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>健康咨讯</el-breadcrumb-item>
            <el-breadcrumb-item>健康咨讯</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="标题" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item" @keyup.enter.native="findPage"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="title" label="标题" align="center"></el-table-column>
                <el-table-column prop="content" label="内容" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="info" size="mini"
                                   @click="handleInfo(scope.row)">查看详情
                        </el-button>
                        <el-button v-if="user.roles[0].id==1" type="primary" size="mini"
                                   @click="handleUpdate(scope.row)">编辑
                        </el-button>
                        <el-button v-if="user.roles[0].id==1" type="danger" size="mini"
                                   @click="handleDelete(scope.row)">删除
                        </el-button>
                    </template>

                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑检查项" :visible.sync="dialogFormVisibleEdit">
                    <el-form ref="dataEditForm" :model="information" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col>
                                <el-form-item label="标题" prop="title">
                                    <el-input v-model="information.title"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12"
                                    v-if="user.roles!=null && user.roles[0].id==1">
                                <el-form-item label="健康管理师" prop="userList">
                                    <el-select v-model="information.user.id">
                                        <el-option v-for="(user,index) in userList" :label="user.username"
                                                   :value="user.id"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col>
                                <el-form-item label="发布时间" required>
                                    <el-date-picker
                                            v-model="information.releaseTime"
                                            type="date"
                                            placeholder="选择日期">
                                    </el-date-picker>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="内容" prop="content">
                                    <el-input v-model="information.content" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisibleEdit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit('dataEditForm')">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index_copy.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data: {
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            dataList: [],//当前页要展示的分页列表数据
            information: {
                user: {
                    id: ''
                }
            },//表单数据
            user: {},
            userList: [],
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisibleEdit: false,//编辑表单是否可见
            rules: {//校验规则
                title: [{required: true, message: '标题为必填项', trigger: 'blur'}],
                content: [{required: true, message: '内容为必填项', trigger: 'blur'}],
                releaseTime: [{required: true, message: '发布时间为必填项', trigger: 'change'}]
            }
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage()
        },
        methods: {
            //日期转字符串格式
            formatDate(dateStr) {
                if (dateStr != null) {
                    let newdateStr = dateStr.replace(/\d+(?=-[^-]+$)/, (a) => (parseInt(a, 10) - 1));  //字符串处理(月份)
                    let nums = newdateStr.match(/\d+/g);  //取数
                    let date = eval('new Date(' + nums + ')');  //转换
                    //重组
                    let datetime =
                        date.getFullYear() + "年" +  //年
                        ((date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1)) + "月" +  //月
                        (date.getDate() > 9 ? date.getDate() : ("0" + date.getDate())) + "日";  //日
                    return datetime;
                }

            },
            //编辑
            handleEdit() {
                this.$refs['dataEditForm'].validate((valid) => {
                    if (valid) {
                        //发送添加请求
                        axios.post('/information/editInformation.do', this.information).then((result) => {
                            if (result.data.flag) {
                                this.$message.success(result.data.message)
                                //清空表单
                                this.dialogFormVisibleEdit = false;
                                this.findPage();
                            } else {
                                this.$message.error(result.data.message)
                            }
                        }).finally(() => {
                            //重新发送请求查询分页数据
                            this.findPage();
                        });
                    } else {
                        //表单校验失败
                        // this.$message.error("表单数据校验失败");
                        return false;
                    }
                });
            },
            //查看详情
            handleInfo(row) {
                localStorage.setItem("informationId", row.id)
                window.location.href = 'informationDetail.html'
            },
            //添加
            handleAdd() {
            },
            //分页查询
            async findPage() {
                //获取用户
                this.user = JSON.parse(localStorage.getItem('userInfo'))
                let result = await axios.post('/information/findPage.do', this.pagination);
                this.dataList = result.data.rows
                this.pagination.total = result.data.total
                //查询所有健康管理师
                let res = await axios.get('/user/findUserRoleAll.do');
                this.userList = res.data.data
            },
            // 重置表单
            resetForm() {
            },
            // 弹出添加窗口
            handleCreate() {

            },
            // 弹出编辑窗口
            handleUpdate(row) {
                this.information = row
                this.dialogFormVisibleEdit = true;
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage
                this.findPage()

            },
            // 删除
            handleDelete(row) {
                this.$confirm('确认删除吗？', '提示', {type: 'warning'}).then(() => {
                    axios.get("/information/deleteInformation.do?id=" + row.id).then((result) => {
                        if (!result.data.flag) {
                            //删除失败
                            this.$message.error(result.data.message)
                        } else {
                            //删除成功
                            this.$message({
                                message: result.data.message,
                                type: 'success'
                            });
                            this.findPage();
                        }
                    }).catch((error) => {
                        this.showMessage(error);
                    });
                }).catch((error) => {
                    //点击取消按钮执行此代码
                    this.$message('操作已取消');
                })
            }
        }
    })
</script>
</html>